package com.kensure.shike.changshang.service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import co.kensure.exception.BusinessExceptionUtil;
import co.kensure.exception.ParamUtils;
import co.kensure.frame.JSBaseService;
import co.kensure.mem.MapUtils;
import co.kensure.mem.PageInfo;

import com.kensure.basekey.BaseKeyService;
import com.kensure.shike.changshang.dao.ChangShangDao;
import com.kensure.shike.changshang.model.ChangShang;
import com.kensure.shike.user.model.SKUser;
import com.kensure.shike.user.service.SKUserService;

/**
 * 厂商服务实现类
 * 
 * @author fankd created on 2019-10-22
 * @since
 */
@Service
public class ChangShangService extends JSBaseService {

	@Resource
	private ChangShangDao dao;
	@Resource
	private BaseKeyService baseKeyService;
	@Resource
	private SKUserService sKUserService;

	public ChangShang selectOne(Long id) {
		return dao.selectOne(id);
	}

	public List<ChangShang> selectByIds(Collection<Long> ids) {
		return dao.selectByIds(ids);
	}

	public List<ChangShang> selectAll() {
		return dao.selectAll();
	}

	public List<ChangShang> selectByWhere(Map<String, Object> parameters) {
		return dao.selectByWhere(parameters);
	}

	public long selectCount() {
		return dao.selectCount();
	}

	public long selectCountByWhere(Map<String, Object> parameters) {
		return dao.selectCountByWhere(parameters);
	}

	public boolean insert(ChangShang obj) {
		super.beforeInsert(obj);
		obj.setId(baseKeyService.getKey("changshang"));
		return dao.insert(obj);
	}

	public boolean update(ChangShang obj) {
		return dao.update(obj);
	}

	public boolean updateByMap(Map<String, Object> params) {
		return dao.updateByMap(params);
	}

	public boolean delete(Long id) {
		return dao.delete(id);
	}

	public boolean deleteMulti(Collection<Long> ids) {
		return dao.deleteMulti(ids);
	}

	public boolean deleteByWhere(Map<String, Object> parameters) {
		return dao.deleteByWhere(parameters);
	}

	public List<ChangShang> selectByWhere(PageInfo page) {
		Map<String, Object> parameters = MapUtils.genMap("orderby", "id desc");
		MapUtils.putPageInfo(parameters, page);
		List<ChangShang> list = selectByWhere(parameters);
		for (ChangShang cs : list) {
			SKUser skuser = sKUserService.selectOne(cs.getUserid());
			cs.setUser(skuser);
		}

		return list;
	}
	
	/**
	 * 根据用户或者厂商
	 * @param userid
	 * @return
	 */
	public List<ChangShang> selectByUserId(Long userid) {
		Map<String, Object> parameters = MapUtils.genMap("userid",userid);
		List<ChangShang> list = selectByWhere(parameters);
		return list;
	}

	public void add(ChangShang obj) {
		ParamUtils.isBlankThrewException(obj.getName(), "名称不能为空");
		ParamUtils.isBlankThrewException(obj.getUserid(), "关联用户不能为空");
		SKUser skuser = sKUserService.selectOne(obj.getUserid());
		if(skuser == null){
			BusinessExceptionUtil.threwException("找不到该用户");
		}
		
		if (obj.getId() != null) {
			update(obj);
		} else {
			insert(obj);
		}
	}

}
